﻿using AutoMapper;
using Sprout.Core.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sprout.Core.Utils
{
    /// <summary>
    /// 层级帮助类
    /// 可转为树
    /// </summary>
    public class LevelHelper
    {
        #region BaseLevel
        /// <summary>
        /// 转成树形
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        public static IList<T> MapperTree<T>(IList<T> items)
            where T : BaseLevel<T>
        {
            List<T> result = new List<T>();
            result = items.Where(n => string.IsNullOrEmpty(n.ParentId) || "0".Equals(n.ParentId)).ToList();
            foreach (var menu in result)
            {
                menu.Children = GetChildren<T>(items, menu.Id);
            }

            return result;
        }
        /// <summary>
        /// 获取子项
        /// </summary>
        /// <param name="items"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        private static IList<T> GetChildren<T>(IList<T> items, string parentId)
            where T : BaseLevel<T>
        {
            IList<T> list = items.Where(n => n.ParentId.ToString() == parentId).ToList();
            if (list == null || list.Count == 0)
            {
                return null;
            }
            foreach (var item in list)
            {
                item.Children = GetChildren<T>(items, item.Id);
            }
            return list;
        }

        /// <summary>
        /// 数据补全
        /// </summary>
        /// <param name="menus"></param>
        /// <param name="allMenus">完整集合</param>
        /// <returns></returns>
        public static IList<T> Completion<T>(IList<T> menus, IList<T> allMenus)
            where T : BaseLevel<T>
        {
            HashSet<string> parentIdHs = new HashSet<string>();
            var childrens = menus.Where(n => !string.IsNullOrEmpty(n.ParentId) && !"0".Equals(n.ParentId)).ToList();
            foreach (var menu in childrens)
            {
                if (parentIdHs.Contains(menu.ParentId) == false)
                {
                    if (!string.IsNullOrEmpty(menu.ParentId) && !"0".Equals(menu.ParentId))
                    {
                        var parentMenu = menus.Where(n => n.Id == menu.ParentId).FirstOrDefault();
                        if (parentMenu == null)
                        {
                            parentMenu = allMenus.Where(n => n.Id == menu.ParentId).FirstOrDefault();
                            if (parentMenu != null)
                            {
                                menus.Add(parentMenu);
                            }
                        }
                    }
                    parentIdHs.Add(menu.ParentId);
                }
            }
            menus = menus.OrderBy(n => n.Sort).ToList();
            return menus;
        }
        #endregion

        #region BaseLevelByNumber
        /// <summary>
        /// 转成树形
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        public static IList<T> MapperTreeByNumber<T>(IList<T> items)
            where T : BaseLevelByNumber<T>
        {
            List<T> result = new List<T>();
            result = items.Where(n => n.ParentId == 0).ToList();
            foreach (var menu in result)
            {
                menu.Children = GetChildrenByNumber<T>(items, menu.Id);
            }

            return result;
        }
        /// <summary>
        /// 获取子项
        /// </summary>
        /// <param name="items"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        private static IList<T> GetChildrenByNumber<T>(IList<T> items, long parentId)
            where T : BaseLevelByNumber<T>
        {
            IList<T> list = items.Where(n => n.ParentId == parentId).ToList();
            if (list == null || list.Count == 0)
            {
                return null;
            }
            foreach (var item in list)
            {
                item.Children = GetChildrenByNumber<T>(items, item.Id);
            }
            return list;
        }

        /// <summary>
        /// 补全
        /// </summary>
        /// <param name="menus"></param>
        /// <param name="allMenus">完整集合</param>
        /// <returns></returns>
        public static IList<T> CompletionByNumber<T>(IList<T> menus, IList<T> allMenus)
            where T : BaseLevelByNumber<T>
        {
            HashSet<long> parentIdHs = new HashSet<long>();
            var childrens = menus.Where(n => n.ParentId != 0).ToList();
            foreach (var menu in childrens)
            {
                if (parentIdHs.Contains(menu.ParentId) == false)
                {
                    if (menu.ParentId != 0)
                    {
                        var parentMenu = menus.Where(n => n.Id == menu.ParentId).FirstOrDefault();
                        if (parentMenu == null)
                        {
                            parentMenu = allMenus.Where(n => n.Id == menu.ParentId).FirstOrDefault();
                            if (parentMenu != null)
                            {
                                menus.Add(parentMenu);
                            }
                        }
                    }
                    parentIdHs.Add(menu.ParentId);
                }
            }
            menus = menus.OrderBy(n => n.Sort).ToList();
            return menus;
        }


        #endregion
    }
}
